﻿using MyConvert;
using MyDateTime;
using MyRegister;
using MySQLite.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MySQLite.DataHelp
{
    /// <summary>
    /// 注册问题
    /// </summary>
    public class RegisterHelper
    {
        /// <summary>
        /// 检测激活信息
        /// </summary>
        /// <param name="days"></param>
        /// <returns></returns>
        public static bool IsRegister(out int days)
        {
            days = 15;
            try
            {
                string strSql = " select Guid, ActiveTime, RegisterCode, RegisterTime from T_Register ";
                DataTable dt = MySQLiteHelper.Instance.ExecuteSqlForDataTable(strSql);
                RegisterModel versionData = ModelConvert.ConvertToModel<RegisterModel>(dt);
                string currentTime = MyDateTimeHelper.CurrentSecondDateTime;
                if (versionData == null || string.IsNullOrEmpty(versionData.Guid))
                {
                    InsertVersion();
                    return false;
                }
                else
                {
                    if (!string.IsNullOrEmpty(versionData.RegisterCode) && VerifyRegisterCode.CheckRegisterCode(versionData.Guid, versionData.RegisterCode))
                    {
                        return true;
                    }
                    else
                    {
                        if (DateTime.TryParse(versionData.ActiveTime, out DateTime date1))
                        {
                            days = 15 - DateTime.Now.Subtract(date1).Days;
                            return false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return false;
        }

        /// <summary>
        /// 更新注册码
        /// </summary>
        /// <param name="registerCode"></param>
        /// <returns></returns>
        public static bool UpdateRegisterCode(string registerCode)
        {
            try
            {
                string strSql = $" update T_Register set RegisterCode='{registerCode}',RegisterTime='{MyDateTimeHelper.CurrentSecondDateTime}' where ActiveTime<>'' ";
                return MySQLiteHelper.Instance.ExecuteSql(strSql);
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        #region 帮助类


        /// <summary>
        /// 插入软件激活信息
        /// </summary>
        /// <returns></returns>
        static bool InsertVersion()
        {
            try
            {
                string strSql = $" insert into T_Register(Guid, ActiveTime) values('{Guid.NewGuid().ToString("N")}', '{MyDateTimeHelper.CurrentSecondDateTime}')";
                return MySQLiteHelper.Instance.ExecuteSql(strSql);
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        #endregion 帮助类
    }

}
